﻿using ExcelDna.Integration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ExcelFunc
{
    class IOUtility
    {

        [ExcelFunction(Description = "获取指定目录下的子文件夹")]
        public static string[,] GetSubFolder(string srcFolder,string containsText,bool isSearchAllDirectory)
        {
            IEnumerable<string> subfolders;
            if (isSearchAllDirectory)
            {
                subfolders = Directory.EnumerateDirectories(srcFolder, "*", SearchOption.AllDirectories).Where(s => iscontainsText(s, containsText));
            }
            else
            {
                subfolders = Directory.EnumerateDirectories(srcFolder, "*", SearchOption.TopDirectoryOnly).Where(s=>iscontainsText(s,containsText));
            }

            int resultCount = subfolders.Count();
            string[,] resultArr = new string[resultCount, 1];
            int i = 0;
            foreach (string item in subfolders)
            {
                resultArr[i, 1] = item;
            }
            return resultArr;

        }

        private static bool iscontainsText(string s,string containstext)
        {
            if (string.IsNullOrEmpty(containstext))
            {
                return true;
            }
            else
            {
                return Regex.IsMatch(s, containstext);
            }

        }

        private static bool IscontainsText(string arg,string containsText)
        {
            throw new NotImplementedException();
        }

        //private static Func<string, bool> IscontainsText(string containsText)
        //{
        //    throw new NotImplementedException();
        //}

        //private static Func<string, bool> IscontainsText(string containsText)
        //{
        //    throw new NotImplementedException();
        //}

        //private static bool IscontainsText(string arg)
        //{
        //    if (containsText is ExcelMissing)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}




        //private static Func<string, bool> IscontainsText()
        //{
        //        if (containsText is ExcelMissing)
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            return false;
        //        }
        //}

        //private static Func<string, bool> IscontainsText(string s)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
